﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.ColorSpaces;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Normalize
    /// </summary>
    public partial class FormNormalize : FormFilter
    {
        protected Normalize m_Normalize = new Normalize();
        protected Normalization_Mode normalizationMode = Normalization_Mode.RGBUseRange;

        /// <summary>
        /// Default constructor
        /// </summary>
        public FormNormalize()
        {
            InitializeComponent();
            InitializeNormalizationModes();
            this.cobNormalizationMode.DropDownStyle = ComboBoxStyle.DropDownList;
            
            this.trackbAmount.Minimum = 0;
            this.trackbAmount.Maximum = 255;

            this.trackbAmount.ValueChanged += new EventHandler(trackbAmount_ValueChanged);
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

        }

        public FormNormalize(Normalization_Mode _normalizationMode)
            : this()
        {
            cobNormalizationMode.SelectedItem = typeof(YUV);
        }

        void trackbAmount_ValueChanged(object sender, EventArgs e)
        {
            Image image = (Image)m_ImageOriginal.Clone();
            Normalize((Bitmap)image);
            pictureBox1.BackgroundImage = image;
            pictureBox1.Invalidate();
        }

        /// <summary>
        /// Normalization mode
        /// </summary>
        private void InitializeNormalizationModes()
        {
            foreach (Normalization_Mode mode in Enum.GetValues(typeof(Normalization_Mode)))
            {
                this.cobNormalizationMode.Items.Add(mode);
            }
            this.cobNormalizationMode.SelectedItem = Normalization_Mode.RGBUseRange;
        }

        /// <summary>
        /// Normalize
        /// </summary>
        /// <param name="bitmap"></param>
        private void Normalize(Bitmap bitmap)
        {
            m_Normalize.NormalizationMode = (Normalization_Mode)cobNormalizationMode.SelectedItem;
            m_Normalize.Threshold = (int)trackbAmount.Value;
            m_Normalize.Execute(bitmap).Dispose();
        }

        private void cbOK_Click(object sender, EventArgs e)
        {
            Image image = (Image)m_ImageOriginal.Clone();
            Normalize((Bitmap)image);
            MainFormSingleton.ActivePictureBox.BackgroundImage = image;
        }
    }
}
